package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.SucursalDAO;
import com.bit.cfdi.web.domain.Sucursal;

@Repository
public class SucursalDAOImpl extends HibernateDaoSupport implements SucursalDAO {

	@Autowired
	@Transactional public void init(final SessionFactory sessionFactory) throws DataAccessException {
		setSessionFactory(sessionFactory);
	}
	
	@Override
	@Transactional public void save(Sucursal negocio) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(negocio);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public Sucursal getById(Long id) throws DataAccessException {
		List<Sucursal> sucursales = getHibernateTemplate().find(
				" from Sucursal as s" +
				" where s.idSucursal", id);
		return sucursales.size()>0?sucursales.get(0):null;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional public List<Sucursal> getAll() throws DataAccessException {
		List<Sucursal> sucursales = getHibernateTemplate().find(
				" from Sucursal as s");
		return sucursales;
	}

}
